//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using System.Resources;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using System.Windows.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using OpenLS.UI;

namespace OpenLS.Media
{
    public class IconProvider : IIconProvider
    {
        ResourceManager _man;

        ResourceManager Manager
        {
            get
            {
                if (_man == null)
                    _man = new ResourceManager("OpenLS.Media.g", typeof(IconProvider).Assembly);

                return _man;
            }
        }
        
        public object GetIcon(ICommand command)
        {
            var cmd = command as RoutedCommand;
            if (cmd == null)
                return null;
            Manager.IgnoreCase = true;
            using (Stream stream = Manager.GetStream(cmd.Name + ".png"))
            {
                if (stream == null)
                    return null;
                var bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = stream;
                bitmap.EndInit();
                var image = new Image {Source = bitmap};
                return image;

            }

        }
    }
}
